package com.leetCode.problem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-11-04 16:06:22
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-11-04     张李鑫                     1.0         1.0 Version
 */
public class Problem23 {
    /**
     * 给你一个链表数组，每个链表都已经按升序排列。
     * <p>
     * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
     */

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static class ListNodeComparator implements Comparator<ListNode>{

        @Override
        public int compare(ListNode o1, ListNode o2) {
            return o1.val-o2.val;
        }
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(4);
        ListNode listNode2 = new ListNode(5);
        listNode.next=listNode1;
        listNode1.next=listNode2;

        ListNode listNode3 = new ListNode(1);
        ListNode listNode4 = new ListNode(3);
        ListNode listNode5 = new ListNode(4);
        listNode3.next=listNode4;
        listNode4.next=listNode5;

        ListNode [] listNodes={};

        ListNode node = mergeKLists(listNodes);
        System.out.println(123);
    }

    public static ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length < 1) {
            return null;
        }
        PriorityQueue<ListNode> listNodes = new PriorityQueue<>(new ListNodeComparator());

        for (int i = 0; i < lists.length; i++) {
            if (lists[i]!=null){
                listNodes.add(lists[i]);
            }
        }
        ListNode poll1 = listNodes.poll();
        if (poll1!=null&&poll1.next!=null){
            listNodes.add(poll1.next);
        }
        ListNode listNode=poll1;
        ListNode cur=listNode;


        while (!listNodes.isEmpty()){
            ListNode poll = listNodes.poll();
            cur.next=poll;
            cur=cur.next;
            if (poll.next!=null){
                listNodes.add(poll.next);
            }
        }
        return listNode;
    }





    public static ListNode mergeKLists1(ListNode[] lists) {
        if (lists == null || lists.length < 1) {
            return null;
        }
        if (lists.length == 1) {
            return lists[0];
        }
        ListNode merge = lists[0];
        for (int i = 1; i < lists.length; i++) {
            merge = merge(merge, lists[i]);
        }
        return merge;
    }

    public static ListNode merge(ListNode l1, ListNode l2) {
        if (l1 == null || l2 == null) {
            return l1 != null ? l1 : l2;
        }
        ListNode head = l1.val > l2.val ? l2 : l1;
        l1 = l1 == head ? l1.next : l1;
        l2 = l2 == head ? l2.next : l2;

        ListNode cur = head;
        while (l1 != null && l2 != null) {
            boolean flag = l1.val < l2.val;
            cur.next = flag ? l1 : l2;
            if (flag) {
                l1 = l1.next;
            } else {
                l2 = l2.next;
            }
            cur = cur.next;
        }

        while (l1 != null) {
            cur.next = l1;
            cur = cur.next;
            l1 = l1.next;
        }
        while (l2 != null) {
            cur.next = l2;
            cur = cur.next;
            l2 = l2.next;
        }
        return head;
    }


}
